package net.dreamlu.common;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.beetl.ext.jfinal.BeetlRenderFactory;

import com.alibaba.druid.filter.logging.Slf4jLogFilter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.upload.OreillyCos;

import net.dreamlu.controller.AdminController;
import net.dreamlu.controller.AjaxController;
import net.dreamlu.controller.BeetlController;
import net.dreamlu.controller.EmailController;
import net.dreamlu.controller.ImageController;
import net.dreamlu.controller.IndexController;
import net.dreamlu.controller.UeditorController;
import net.dreamlu.ext.UpFileRenamePolicy;
import net.dreamlu.ext.plugin.log.Slf4jLogFactory;
import net.dreamlu.handler.StaticHandler;
import net.dreamlu.model.Blog;
import net.dreamlu.model.TUser;

public class WebConfig extends JFinalConfig {

	// 开发模式
	private boolean isdev = false;

	@Override
	public void configConstant(Constants me) {
		// 加载配置文件
		loadPropertyFile("config.txt");

		isdev = getPropertyToBoolean("isdev", false);
		me.setDevMode(isdev);

		// 修改文件上传的默认限制
		me.setMaxPostSize(1024 * 1024 * 200);
//		me.setUploadedFileSaveDirectory("test");
		
		// 设置Slf4日志
		me.setLoggerFactory(new Slf4jLogFactory());
		// beetl模版配置工厂
		BeetlRenderFactory beetlRenderFactory = new BeetlRenderFactory();
		me.setMainRenderFactory(beetlRenderFactory);

		// 401,403,404,500错误代码处理
		me.setError401View("/common/404.html");
		me.setError403View("/common/404.html");
		me.setError404View("/common/404.html");
		me.setError500View("/common/404.html");
	}

	@Override
	public void configRoute(Routes me) {
		me.add("/", IndexController.class, "front");
		me.add("/image", ImageController.class, "front/image");
		me.add("/ueditor", UeditorController.class, "front/ueditor");

		me.add("/ajax", AjaxController.class);

		// beetl测试
		me.add("/beetl", BeetlController.class, "front");
		me.add("/email", EmailController.class);

		// 管理后台控制器
		me.add("/admin", AdminController.class);
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new StaticHandler("/static"));
//		me.add(new FakeStaticHandler());
	}

	@Override
	public void configInterceptor(Interceptors me) {
		
	}

	@Override
	public void configPlugin(Plugins me) {
		// 数据库信息
		String jdbcUrl  = getProperty("db.jdbcUrl");
		String user     = getProperty("db.user");
		String password = getProperty("db.password");

		// 配置Druid数据库连接池插件
		DruidPlugin dp = new DruidPlugin(jdbcUrl, user, password);
		dp.addFilter(new StatFilter()).addFilter(new Slf4jLogFilter());
		WallFilter wall = new WallFilter();
		wall.setDbType("mysql");
		dp.addFilter(wall);
		me.add(dp);

		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		arp.addMapping("t_user", "id", TUser.class);
		arp.addMapping("blog", Blog.class);
		arp.setShowSql(isdev);

		me.add(arp);

		// ehcahce插件配置
		me.add(new EhCachePlugin());
	}

	@Override
	public void afterJFinalStart() {
		super.afterJFinalStart();
		//设置文件上传重命名策略
		OreillyCos.setFileRenamePolicy(new UpFileRenamePolicy());

		// 在JFinal启动时，beetl变量中加入启动时间 ${startTime!, "yyyy-MM-dd HH:mm:ss"}
		Map<String, Object> sharedVars = new HashMap<String, Object>();
		String startTime = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
		sharedVars.put("startTime", startTime);

		BeetlRenderFactory.groupTemplate.setSharedVars(sharedVars);
	}

}
